home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / oop_tp55.zip / ROBOTSEG.PAS < prev    next >
Pascal/Delphi Source File  |  1990-02-20  |  4KB  |  154 lines

  1. unit RobotSeg;  { Listing 10-4 }
  2.  
  3. interface
  4.  
  5. uses Graph, Mouse, Crt;
  6.  
  7. const
  8.      PI : real = 3.14159;
  9.  
  10. type
  11.  
  12. Degrees = 0..359;
  13.  
  14. Point = object
  15.       X,Y : Integer;
  16.       procedure Init(NewX,NewY : Integer);
  17.       procedure Show;
  18.       procedure Hide;
  19.       end;
  20.  
  21.  
  22. Segment = object
  23.         Anchor      : Point;
  24.         BusyEnd     : Point;
  25.         Length      : integer;
  26.         Orientation : Real;
  27.         RotateStatus: Boolean;
  28.         constructor Init( AnchorX, AnchorY, SegLen : Integer;
  29.                         Position : Degrees;
  30.                         Rotatable : Boolean );
  31.         procedure MoveAxial( var i : integer ); virtual;
  32.         procedure MoveAnchor( NewX, NewY : integer );
  33.         procedure Rotate( i : integer );
  34.         procedure Show;
  35.         procedure Hide;
  36.         function RotateQ : Boolean;
  37.         end;
  38.  
  39. function Distance( P1, P2 : Point ) : real;
  40.  
  41.  
  42. implementation
  43.  
  44. function Distance( P1, P2 : Point ) : real;
  45. begin
  46.      Distance :=
  47.        Sqrt( Sqr( P1.X - P2.X + 0.01) + Sqr( P1.Y - P2.Y + 0.01) );
  48. end;
  49.  
  50. procedure Point.Init( NewX, NewY : integer );
  51. begin
  52.      X := NewX;
  53.      Y := NewY;
  54. end;
  55.  
  56. procedure Point.Show;
  57. begin
  58.      Graph.Rectangle( X-8, Y-8, X+8, Y+8);
  59. end;
  60.  
  61. procedure Point.Hide;
  62. var TmpClr : integer;
  63. begin
  64.      TmpClr := Graph.GetColor;
  65.      Graph.SetColor(GetBkColor);
  66.      Show;
  67.      Graph.SetColor(TmpClr);
  68. end;
  69.  
  70. constructor Segment.Init( AnchorX, AnchorY, SegLen : Integer;
  71.                         Position : Degrees;
  72.                         Rotatable : Boolean );
  73. var BX, BY : integer;
  74. begin
  75.      Anchor.Init( AnchorX, AnchorY );
  76.      RotateStatus := Rotatable;
  77.      Orientation := Position * PI / 180.0;
  78.      Length := SegLen;
  79.      BX := Round(Anchor.X + cos(Orientation)*SegLen);
  80.      BY := Round(Anchor.Y - sin(Orientation)*SegLen);
  81.      BusyEnd.Init( BX, BY );
  82. end;
  83.  
  84. procedure Segment.MoveAxial( var i : integer );
  85. var D : real;
  86. begin
  87.      Hide;
  88.      D := Distance( Anchor, BusyEnd );
  89.      if D-i >= Length then
  90.         D := Length+i;
  91.      BusyEnd.X := Round(Anchor.X + cos(Orientation)*(D-i));
  92.      BusyEnd.Y := Round(Anchor.Y - sin(Orientation)*(D-i));
  93.      Show;
  94. end;
  95.  
  96. procedure Segment.MoveAnchor( NewX, NewY : integer );
  97. var
  98.    DeltaX, DeltaY : integer;
  99. begin
  100.      Hide;
  101.      with Anchor do
  102.           begin
  103.           DeltaX := X - NewX;
  104.           DeltaY := Y - NewY;
  105.           X := NewX;
  106.           Y := NewY;
  107.           end;
  108.      with BusyEnd do
  109.           begin
  110.           X := X + DeltaX;
  111.           Y := Y + DeltaY;
  112.           end;
  113.      Show;
  114. end;
  115.  
  116. procedure Segment.Rotate( i : integer );
  117. begin
  118.      Hide;
  119.      Orientation := Orientation + ( i * ( PI / 90.0 ) ) ;
  120.      Show;
  121. end;
  122.  
  123. function Segment.RotateQ : Boolean;
  124. begin
  125.      RotateQ := RotateStatus;
  126. end;
  127.  
  128. procedure Segment.Show;
  129. var
  130.     D : real;
  131.     FX, FY : integer;
  132. begin
  133.      D := Distance( Anchor, BusyEnd );
  134.      BusyEnd.X := Round(Anchor.X + cos(Orientation)*D);
  135.      BusyEnd.Y := Round(Anchor.Y - sin(Orientation)*D);
  136.      FX := Round(BusyEnd.X - cos(Orientation)*Length);
  137.      FY := Round(BusyEnd.Y + sin(Orientation)*Length);
  138.      Graph.Line( FX, FY, BusyEnd.X, BusyEnd.Y );
  139.      Graph.Circle( BusyEnd.X, BusyEnd.Y, 2);
  140. end;
  141.  
  142. procedure Segment.Hide;
  143. var
  144.    TmpClr : integer;
  145.    LST : LineSettingsType;
  146. begin
  147.      TmpClr := Graph.GetColor;
  148.      Graph.SetColor(GetBkColor);
  149.      Show;
  150.      Graph.SetColor(TmpClr);
  151.  
  152. end;
  153.  
  154. end.